package com.nd.te.province;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class selandgroup {
    public static class selmapper extends Mapper<LongWritable,Text,bean,IntWritable>{
        protected void map (LongWritable key, Text line, Mapper<LongWritable, Text, bean, IntWritable>.Context context) throws IOException, InterruptedException{
            String line1 = new String(line.getBytes(),0,line.getLength(),"UTF-8");
            String[] cc = line1.split(",");
            bean be =new bean();
            be.set(Integer.parseInt(cc[0]),cc[2],cc[4]);
            context.write(be,new IntWritable(1));
        }
    }

    public static class selreducer extends Reducer<bean,IntWritable,bean,IntWritable>{
        protected void reduce(bean key,Iterable<IntWritable> values,Reducer<bean,IntWritable,bean,IntWritable>.Context context)throws IOException, InterruptedException{
            int count =0;
            for (IntWritable e: values) {
                count += e.get();
            }
            context.write(key, new IntWritable(count));
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException{
        args=new String[]{"data/input/dataprocess.txt","data/dataselect2"};

        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);

        job.setMapperClass(selmapper.class);
        job.setReducerClass(selreducer.class);

        job.setMapOutputKeyClass(bean.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(bean.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}
